package org.jhub1.system.xmpp.commands;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.jhub1.system.xmpp.config.ConfigAdapter;
import org.jhub1.system.xmpp.proxy.ProcessResponse;
import org.jhub1.system.xmpp.proxy.ProcessorSession;
import org.jhub1.system.xmpp.proxy.XMPPProxy;
//import org.jhub1.system.xmpp.log.LogAdapter;
import org.jivesoftware.smack.XMPPException;

public class MessagesProcessor {

	private static ConfigAdapter config;

	// private static LogAdapter log =
	// LogAdapter.getLog(MessagesProcessor.class);

	public MessagesProcessor(ConfigAdapter config) {
		MessagesProcessor.config = config;
		ProcessorSession.getInstance();
	}

	public boolean activateSession(String msg, String jid) {
		boolean returnVal = false;
		if (msg.startsWith("?StartSession")) {
			String[] params = msg.split("\\ ");
			int parLen = params.length;
			switch (parLen) {
			case 2:
				if (params[1].equals(config.getSessionPassword())) {
					if (!ProcessorSession.isActive()) {
						ProcessorSession.setTimeout(15);
						returnVal = true;
					}
				}
				break;
			case 3:
				if (params[1].equals(config.getSessionPassword())) {
					int sessionTime;
					if (!ProcessorSession.isActive()) {
						try {
							sessionTime = Integer.parseInt(params[2]);
						} catch (NumberFormatException e) {
							sessionTime = 15;
						}
						ProcessorSession.setTimeout(sessionTime);
						returnVal = true;
					}
				}
				break;
			}
		} else {
			returnVal = false;
		}
		return returnVal;
	}

	public boolean doCommand(String msg, String jid) throws XMPPException,
			IOException {
		ProcessorSession.prolongSession();
		if (msg.equalsIgnoreCase("bye")) {
			ProcessorSession.killSession();
			XMPPProxy.sendMessage(jid,
					"JHUB1(bye): Session is inactive. Goodbye.");
			return true;
		} else if (msg.equalsIgnoreCase("uptime")) {
			Process p = Runtime.getRuntime().exec("uptime");
			BufferedReader in = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			String line;
			String resp = "";
			while ((line = in.readLine()) != null) {
				resp = resp + line;
			}
			XMPPProxy.sendMessage(jid, "JHUB1(uptime): " + resp);
			return true;
		} else if (msg.equalsIgnoreCase("mirroriq")) {
			ProcessorSession.setMirrorIQ(true);
			XMPPProxy
					.sendMessage(jid,
							"JHUB1(mirroriq): All received IQ elements will be displayed as a message.");
			return true;
		} else if (msg.equalsIgnoreCase("nomirroriq")) {
			ProcessorSession.setMirrorIQ(false);
			XMPPProxy.sendMessage(jid,
					"JHUB1(nomirroriq): IQ will not be sent back.");
			return true;
		} else if (msg.equalsIgnoreCase("list") || msg.equalsIgnoreCase("help")) {
			StringBuilder message = new StringBuilder();
			message.append("JHUB1(help): \n");
			message.append("'uptime' - shows system uptime \n");
			message.append("'mirroriq' - duplicate received IQs as a message \n");
			message.append("'nomirroriq' - stops duplicating received IQs as a message \n");
			message.append("'list'/'help' - displays this message \n");
			message.append("'bye' - always ending session \n");
			XMPPProxy.sendMessage(jid, message.toString());
			return true;
		} else if (msg.startsWith("IQ")) {
			String command = msg.replace("IQ", "");
			CommandExecutor ce = new CommandExecutor(MessagesProcessor.config,
					"MA");
			ProcessResponse pr = ce.executeComTask(command, null);
			if (pr.error) {
				XMPPProxy.sendMessage(jid, pr.errorMessage);
			} else {
				XMPPProxy.sendMessage(jid, pr.getMessageXMLSafe());
			}
			return true;
		} else {
			return false;
		}
	}

}
